package yxy.game.pm2.active.command.belovedhero;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actfavoritewarlordreward;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_心仪武将活动;
import yxy.game.pm2.active.module.actfavoritewarlord.FavouriteTask;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Collection;

/****************************************
 * @name CMD_Active_心仪武将_ActBelovedHeroLoginRewardOneButton
 * @description 心仪武将登录福利一键领取
 * @author 梁健彬
 * @since 2022-07-8
 ***************************************/
@MessageType(proto = Command.ActBelovedHeroLoginRewardOneButton, classtype = ActiveMessage.ActBelovedHeroLoginRewardOneButtonRequest.class)
public class CMD_Active_心仪武将_ActBelovedHeroLoginRewardOneButton extends Command<ActiveMessage.ActBelovedHeroLoginRewardOneButtonRequest> {

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActBelovedHeroLoginRewardOneButtonRequest request) throws Exception {
        String playercode = messageSource.getTag();
        ActiveMessage.ActBelovedHeroLoginRewardOneButtonResponse.Builder response = ActiveMessage.ActBelovedHeroLoginRewardOneButtonResponse.newBuilder();

        try {
            ActiveModule_心仪武将活动 am = ActiveModule_心仪武将活动.get(playercode, false);

            if (am == null) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            if (!am.running()) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            ActBelovedHeroHandle heroHandle = new ActBelovedHeroHandle();

            //todo 遍历所有任务，获取能领取的奖励
            ArrayList<Currency> returnCurrency = new ArrayList<>();

            for (FavouriteTask task : am.getTaskMap().values()) {
                Task freeTaskBean = Task.get(playercode, task.getFreeTaskCode());//免费的任务
                Task preTaskBean = Task.get(playercode, task.getPreTaskCode());//付费的任务

                if (freeTaskBean == null || preTaskBean == null) {
                    continue;
                }

                if (freeTaskBean.isReward() && preTaskBean.isReward()) {
                    continue;
                }

                if (!freeTaskBean.isFinish() && !preTaskBean.isFinish()) {
                    continue;
                }

                actfavoritewarlordreward data = DataCenter.getData(task.getId(), actfavoritewarlordreward.class);//奖励表
                if (data == null) {
                    continue;
                }

                //获取已完成的奖励
                ArrayList<Currency> currencies = heroHandle.getCurs(am, freeTaskBean, preTaskBean, task, playercode, data);
                returnCurrency.addAll(currencies);

            }

            if (returnCurrency == null || returnCurrency.size() <= 0) {
                send(messageSource, response, Result.ActBelovedHeroNoRewardCanGet);//无奖励可领
                return;
            }

            Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(returnCurrency);

            response.addAllItem(items);//返回奖励信息

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, ActiveMessage.ActBelovedHeroLoginRewardOneButtonResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
